package org.example.order.controller;

import org.example.order.cache.OrderCacheList;
import org.example.order.domain.Order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Description: 支付
 * Author: e
 * CreateDate: 2021-08-02
 */
@Controller
@RequestMapping("/pay")
public class PayController {


    @RequestMapping("/{orderId}")
    public String pay(@PathVariable("orderId") Long orderId, Model model) {
        Order order = OrderCacheList.findOrderByOrderId(orderId);
        if (order == null){
            model.addAttribute("msg", orderId + "订单不存在");
            return "fail";
        }
        if (order.getStatus()==2){
            model.addAttribute("msg", orderId + order.getAddition());
            return "fail";
        }
        if (order.getStatus()==1){
            model.addAttribute("msg", orderId + "已经支付，无需重复付款");
            return "fail";
        }
        // 更新数订单状态
        OrderCacheList.updateOrderStatus(orderId,1,"支付成功");
        System.out.println("订单【"+orderId+"】支付成功");
        model.addAttribute("orderId", orderId);
        return "success";
    }
}
